package com.lwl.blog.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwl.blog.core.utils.R;
import com.lwl.blog.system.entity.SystemUserEntity;
import com.lwl.blog.system.service.ISystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lwl
 * @since 22-12-23
 */

@RestController
@RequestMapping("/sysUser")
public class SystemUserController {

    //注入service层
    @Autowired
    private ISystemUserService userService;

    //跳转页面，@RequestBod把请求体中的数据转成独享
    @PostMapping("/page")
    public R page(@RequestBody Page<SystemUserEntity> page) {

        userService.page(page);
        return R.ok().put("data", page);
    }

    //添加用户
    @PostMapping("/save")
    public R save(@RequestBody SystemUserEntity systemUserEntity) {

        //设置创建时间和默认状态
        systemUserEntity.setCreateTime(new Date());
        systemUserEntity.setStatus(1);

        return R.out(userService.save(systemUserEntity));
    }

    //根据id跟新用户
    @PutMapping("/update")
    public R update(@RequestBody SystemUserEntity systemUserEntity) {

        boolean save = userService.updateById(systemUserEntity);
        return R.out(save);
    }

    //用户信息
    @GetMapping("/info")
    public R info(@PathVariable Integer id) {

        SystemUserEntity systemUserEntity = userService.getById(id);
        return R.ok().put("data", systemUserEntity);
    }

    //单个、批量删除
    @DeleteMapping("/batchDel")
    public R batchDel(@RequestBody List<Integer> id) {

        return R.out(userService.removeBatchByIds(id));
    }
}
